package com.zhika.zishi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.LetterChatAdapter;
import com.zhika.zishi.beans.LetterChatBean;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.interfaces.TitleInterface;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by shi-02 on 2015/4/29.
 */
public class LetterChatActivity extends BaseActivity {

    private TitleView titleView;

    private ListView listView;

    private ArrayList<LetterChatBean> letterChatList = new ArrayList<>();

    private LetterChatAdapter letterChatAdapter;

    private EditText contentEdit;

    private TextView sendText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.letter_chat_layout);

        initView();
    }

    private void initView(){
        titleView = (TitleView) findViewById(R.id.letter_chat_title_view);
        titleView.setLeftButtonText("返回");
        titleView.setMiddleRowText("对方昵称");
        titleView.setRightButtonText("更多");
        titleView.setTitleViewInterface(new TitleInterface() {
            @Override
            public void leftTitleListener() {
                finish();
            }

            @Override
            public void middleTitleListener() {

            }

            @Override
            public void rightTitleListener() {

            }
        });

        addTestData();

        listView = (ListView) findViewById(R.id.letter_chat_list_view);
        letterChatAdapter = new LetterChatAdapter(LetterChatActivity.this,letterChatList);
        listView.setAdapter(letterChatAdapter);

        contentEdit = (EditText) findViewById(R.id.letter_chat_content_edit);

        sendText = (TextView) findViewById(R.id.letter_chat_send_text);

        sendText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = contentEdit.getText().toString();
                if (!content.equals("")&&content!=null){
                    LetterChatBean bean = new LetterChatBean();
                    bean.setTypeStr("self");
                    bean.setTimeStr(getCurrentTime());
                    bean.setContentStr(content);

                    letterChatList.add(bean);
                    letterChatAdapter.notifyDataSetChanged();

                    listView.setSelection(letterChatList.size()-1);

                    contentEdit.setText("");
                }
            }
        });
    }

    private void addTestData(){
        LetterChatBean bean = new LetterChatBean();
        bean.setTypeStr("other");
        bean.setTimeStr("18:15");
        bean.setContentStr("这是你的益达");

        letterChatList.add(bean);

        LetterChatBean bean1 = new LetterChatBean();
        bean1.setTimeStr("18:20");
        bean1.setTypeStr("self");
        bean1.setContentStr("不，这是你的益达");

        letterChatList.add(bean1);
    }

    /**
     * 获得系统当前时间
     * @return
     */
    private String getCurrentTime(){
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date date = new Date(System.currentTimeMillis());
        String timeStr = format.format(date);
        return timeStr;
    }
}
